package socketutils;

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

public class Crypt {
	private static final String cleSecrete = "A4t2F7u9H7v5y66i";
	private static final String algorythme = "AES";
	private static final Key cle = new SecretKeySpec(cleSecrete.getBytes(), algorythme);

	public static byte[] encrypte(byte[] texteEnClair) {
		Cipher cipher = null;
		try {
			cipher = Cipher.getInstance(algorythme);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}

		try {
			cipher.init(Cipher.ENCRYPT_MODE, cle);
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}

		byte[] texteCrypte = null;

		try {
			texteCrypte = cipher.doFinal(texteEnClair);
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}

		return texteCrypte;
	}

	public static byte[] decrypte(byte[] texteCrypte) {
		Cipher cipher = null;
		try {
			cipher = Cipher.getInstance(algorythme);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}

		try {
			cipher.init(Cipher.DECRYPT_MODE, cle);
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}

		byte[] texteEnClair = null;

		try {
			texteEnClair = cipher.doFinal(texteCrypte);
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}

		return texteEnClair;
	}
}